package com.hx.learn.tx.jdbc;

import com.hx.learn.tx.jdbc.util.DBUtil;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * @author jxlgcmh
 * @date 2020-05-17 13:20
 * @description
 */
public class Main2 {
    public static void main(String[] args) throws SQLException {

        Connection connection = DBUtil.getConnection();
        /**
         * int TRANSACTION_NONE             = 0;
         * int TRANSACTION_READ_UNCOMMITTED = 1;
         * int TRANSACTION_READ_COMMITTED   = 2;
         * int TRANSACTION_REPEATABLE_READ  = 4
         * int TRANSACTION_SERIALIZABLE     = 8;
         */
        int level = connection.getTransactionIsolation();
        /**
         * 在这里设置事物的隔离级别，是会话级别的，不与数据库服务器设置的冲突
         */
        //connection.setTransactionIsolation(8);
        System.out.println(level);
        try {
            connection.setAutoCommit(false);
            String sql = "insert into user(id,name) values(8,'jack8')";
            String sql2 = "insert into user2(id,name) values(2,'jack2')";
            Statement statement = connection.createStatement();

            statement.execute(sql2);
            statement.execute(sql);

             int a = 2 / 0;

            connection.commit();
            // 直接添加一个异常

        } catch (Exception e) {
            System.out.println("出现异常，回滚");
            connection.rollback();
        } finally {
            if (connection !=null){
                connection.close();
            }
        }

    }
}
